﻿/*
 * Created by SharpDevelop.
 * User: cszhangyi
 * Date: 2016/4/8
 * Time: 15:50
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

using YMTSeller.Model;

namespace YMTSeller
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		private LoginHandler loginHandler;
		private ActivityHandler activityHandler = null;
		ProductHandler productHandler = null;
		private FileUtils fileUtils = new FileUtils();
		List<SimpleModel> list = new List<SimpleModel>();
		
		private string accessToken= "";
		private string userId = "";
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			foreach(var username in fileUtils.ReadFileByline())
			{
				SimpleModel model = new SimpleModel();
				model.Username = username.Split('/')[0];
				model.Password = username.Split('/')[1];
				
				list.Add(model);
			}
			
			username_comboBox.DataSource = list;
			username_comboBox.DisplayMember = "Username";
		}
		
		void Login_buttonClick(object sender, EventArgs e)
		{
			process();
		}
		
		public void process()
		{
			this.result_richText.Text = "用户：" + this.username_comboBox.Text + "正在登录...";
			loginHandler = new LoginHandler(this.username_comboBox.Text, this.password_tb.Text);
			Dictionary<string, string> loginDictionary = loginHandler.login();

			if (loginDictionary["Msg"].Equals("操作成功.") && loginDictionary["Status"].Equals("200"))
			{
				this.result_richText.AppendText("\n" + this.username_comboBox.Text + "登录成功");
				
				if (fileUtils.isFileEmpty())
					fileUtils.WriteFileByline(this.username_comboBox.Text + "/" + this.password_tb.Text + "\n");
				else
					fileUtils.UpdateFile(this.username_comboBox.Text + "/" + this.password_tb.Text);
			}	
			else
			{
				this.result_richText.AppendText("\n" + this.username_comboBox.Text + "登录失败，" + loginDictionary["Msg"]);
				return;
			}
				
			this.accessToken = loginDictionary["AccessToken"];
			this.userId = loginDictionary["UserId"];
		
			activityHandler = new ActivityHandler(this.accessToken, this.userId, this.dateTimePicker1.Value);
			string msg = activityHandler.GetActivityListBySeller();
			this.productHandler = new ProductHandler(this.accessToken, this.userId);
			
			this.result_richText.AppendText("\n" + msg);
		}
		
		private void CanceActivity_ButtonClick(object sender, EventArgs e)
		{
			string msg = activityHandler.closeActivity();
			this.result_richText.AppendText("\n" + msg);
		}
		
		private void CreateActivity_buttonClick(object sender, EventArgs e)
		{
			string msg = activityHandler.createActivity(this.dateTimePicker1.Value);
			this.result_richText.AppendText("\n" + msg);
		}
		void Username_comboBoxSelectedIndexChanged(object sender, EventArgs e)
		{
			SimpleModel coSelected = (SimpleModel)this.username_comboBox.SelectedItem;
			this.password_tb.Text = coSelected.Password;
		}
		void AddProduct_ButtonClick(object sender, EventArgs e)
		{
			this.result_richText.AppendText("\n" + this.productHandler.addProduct(activityHandler.GetTopActivityBySeller()));
		}
	}
}
